Magic quotes

Magic quotes is a controversial feature of the PHP scripting language, which was introduced to help newcomers write functioning SQL commands without requiring manual escaping. It was later described and widely misinterpreted as help to prevent inexperienced developers from writing code which is vulnerable to SQL injection attacks. This feature is officially deprecated as of PHP 5.3.0, and removed in PHP 5.4 due to security concerns.[1]

Contents

Concept

The current revision of the PHP manual mentions the rationale behind magic quotes is to "help [prevent] code written by beginners from being dangerous."[2] It was however originally introduced in PHP2 as php.h compile-time setting for msql and only escaping single quotes "making it easier to pass form data directly to msql queries"[3]. It originally was intended as "convenience feature, not as security feature."[4].[5]

The use scope was expanded in PHP3. Single quotes, double quotes, backslashes and null characters in all user-supplied data all have a backslash prepended to them before being passed to the script in the $_GET, $_REQUEST, $_POST and $_COOKIE global variables. Developers can then in theory use string concatenation safely to construct SQL queries with data provided by the user. (Which was true at best around PHP2 and PHP3, when the primarily supported databases only allowed 1-byte character sets.)

Criticism

Magic quotes were enabled by default in new installations of PHP3 and 4, and since their operation is behind the scenes and not immediately obvious, developers may be unaware of their existence and the potential problems that they can introduce. The PHP documentation points out several pitfalls and recommends that, despite being enabled by default, they should be disabled.[6]

Problems with magic quotes include:

In November 2005 the core PHP developers decided on account of these problems that the magic quotes feature would be removed from PHP 6.[10]

Other approaches

References

  1. ^ "PHP: Magic Quotes". http://php.net/manual/en/security.magicquotes.php. Retrieved 2009-05-02. 
  2. ^ "PHP:Why use magic quotes?". PHP documentation. http://uk.php.net/manual/en/security.magicquotes.why.php. Retrieved 2007-02-19. 
  3. ^ "If the MAGIC_QUOTES variable is defined in the php.h file then these quotes will be automatically escaped making it easier to pass form data directly to msql queries.". http://www.php.net/manual/phpfi2.php#msql_support. Retrieved 1997-04-23. 
  4. ^ "Magic Quotes is oft-understood, even by journeymen PHP programmers". http://www.gthelp.com/showpost.php?s=1f2dcfbf42564c12dcc84d35cbedfc4d&p=184969&postcount=3. 
  5. ^ "Re: [PHP3 what are magic_quotes?"]. PHP-dev mailing list. http://marc.info/?l=php-general&m=93575922621574&w=2. Retrieved 1999-08-27. 
  6. ^ "PHP:Why not to use magic quotes". PHP documentation. http://uk.php.net/manual/en/security.magicquotes.whynot.php. Retrieved 2007-02-19. 
  7. ^ "Quotation marks are double escaped when editing a comment". WordPress issue tracker. http://trac.wordpress.org/ticket/2768. Retrieved 2007-02-19. 
  8. ^ Chris Shiflett. "addslashes() versus mysql_real_escape_string()". http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string. Retrieved 2007-02-19. 
  9. ^ MySQL AB. "Changes in release 5.0.22 (24 May 2006)". MySQL 5.0 Reference Manual. http://dev.mysql.com/doc/refman/5.0/en/news-5-0-22.html. Retrieved 2007-02-19. 
  10. ^ PHP Group (2005-11-12). "Minutes PHP Developers Meeting". http://www.php.net/~derick/meeting-notes.html#magic-quotes. Retrieved 2007-02-19. 
  11. ^ Dan Ragle (2006-04-18). "Introduction to Perl's Taint Mode". webreference.com. http://www.webreference.com/programming/perl/taint/. Retrieved 2007-03-21. 
  12. ^ "Locking Ruby in the Safe". Programming Ruby. http://www.rubycentral.com/book/taint.html. Retrieved 2007-03-21. 
  13. ^ Joel Spolsky (2005-05-11). "Making Wrong Code Look Wrong". Joel on Software: Painless Software Management. http://www.joelonsoftware.com/articles/Wrong.html. Retrieved 2007-02-19. 

External links